"use client";
import React from "react";
import { UploadOutlined } from "@ant-design/icons";
import type { UploadProps } from "antd";
import { Button, message, Upload } from "antd";
import { checkAudioDuration } from "../../tool";

interface UploadSelfProps {
  FilePath?: (url: string) => void;
}

const App: React.FC<UploadSelfProps> = ({ FilePath }) => {
  const props: UploadProps = {
    name: "file",
    action:
      process.env.NEXT_PUBLIC_TYPE == "dev"
        ? "/people/upload"
        : process.env.NEXT_PUBLIC_API_URL_PEOPLE + "/upload",
    maxCount: 1,
    accept: ".wav,.mp3",
    beforeUpload: async (file) => {
      const isValid = await checkAudioDuration(file);
      return isValid || Upload.LIST_IGNORE;
    },
    onRemove: () => {
      FilePath?.("");
      return true;
    },
    onChange(info) {
      if (info.file.status !== "uploading") {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === "done") {
        message.success("上传成功");
        console.log(info.file.response.file_path);
        FilePath?.(info.file.response.file_path);
      } else if (info.file.status === "error") {
        message.error("上传失败");
      }
    },
  };

  return (
    <Upload {...props}>
      <Button icon={<UploadOutlined />}>上传音频</Button>
    </Upload>
  );
};

export default App;
